home *** CD-ROM | disk | FTP | other *** search
/ PC World 2007 March / PCWorld_2007-03_cd.bin / v cisle / httrack / httrack-3.41-2.exe / {app} / libtest / callbacks-example-filename2.c < prev    next >
C/C++ Source or Header  |  2006-06-04  |  4KB  |  128 lines

  1. /*
  2.     How to build: (callback.so or callback.dll)
  3.       With GNU-GCC:
  4.         gcc -O -g3 -Wall -D_REENTRANT -shared -o mycallback.so callbacks-example.c -lhttrack1
  5.       With MS-Visual C++:
  6.         cl -LD -nologo -W3 -Zi -Zp4 -DWIN32 -Fe"mycallback.dll" callbacks-example.c libhttrack1.lib
  7.  
  8.       Note: the httrack library linker option is only necessary when using libhttrack's functions inside the callback
  9.  
  10.     How to use:
  11.       httrack --wrapper mycallback,string1,string2 ..
  12. */
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17.  
  18. /* Standard httrack module includes */
  19. #include "httrack-library.h"
  20. #include "htsopt.h"
  21. #include "htsdefines.h"
  22.  
  23. /* Function definitions */
  24. static int mysavename(t_hts_callbackarg *carg, httrackp *opt, const char* adr_complete, const char* fil_complete, const char* referer_adr, const char* referer_fil, char* save);
  25. static int myend(t_hts_callbackarg *carg, httrackp *opt);
  26.  
  27. /* TOLOWER */
  28. #define TOLOWER_(a) (a >= 'A' && a <= 'Z') ? (a + ('a' - 'A')) : a
  29. #define TOLOWER(a) ( TOLOWER_( (a) ) )
  30.  
  31. /*
  32.   This sample just replaces all occurences of "string1" into "string2"
  33.   string1 and string2 are passed in the callback string:
  34.   httrack --wrapper save-name=callback:mysavename,string1,string2 ..
  35. */
  36.  
  37. typedef struct t_my_userdef {
  38.   char string1[256];
  39.   char string2[256];
  40. } t_my_userdef;
  41.  
  42. /* 
  43. module entry point 
  44. */
  45. EXTERNAL_FUNCTION int hts_plug(t_hts_callbackarg *carg, httrackp *opt, const char* argv) {
  46.   const char *arg = strchr(argv, ',');
  47.   if (arg != NULL)
  48.     arg++;
  49.  
  50.   /* Check args */
  51.   if (arg == NULL || *arg == '\0' || strchr(arg, ',') == NULL) {
  52.     fprintf(stderr, "** callback error: arguments expected or bad arguments\n");
  53.     fprintf(stderr, "usage: httrack --wrapper save-name=callback:mysavename,string1,string2\n");
  54.     fprintf(stderr, "example: httrack --wrapper save-name=callback:mysavename,foo,bar\n");
  55.     return 0;   /* failed */
  56.   } else {
  57.     char *pos = strchr(arg, ',');
  58.     t_my_userdef *userdef = (t_my_userdef*) malloc(sizeof(t_my_userdef));
  59.     char * const string1 = userdef->string1;
  60.     char * const string2 = userdef->string2;
  61.  
  62.     /* Split args */
  63.     fprintf(stderr, "** info: wrapper_init(%s) called!\n", arg);
  64.     fprintf(stderr, "** callback example: changing destination filename word by another one\n");
  65.     string1[0] = string1[1] = '\0';
  66.     strncat(string1, arg, pos - arg);
  67.     strcpy(string2, pos + 1);
  68.     fprintf(stderr, "** callback info: will replace %s by %s in filenames!\n", string1, string2);
  69.  
  70.     /* Plug callback functions */
  71.     CHAIN_FUNCTION(opt, savename, mysavename, userdef);
  72.     CHAIN_FUNCTION(opt, end, myend, userdef);
  73.   }
  74.  
  75.   return 1;  /* success */
  76. }
  77.  
  78. static int myend(t_hts_callbackarg *carg, httrackp *opt) {
  79.   t_my_userdef *userdef = (t_my_userdef*) CALLBACKARG_USERDEF(carg);
  80.  
  81.   fprintf(stderr, "** info: wrapper_exit() called!\n");
  82.   if (userdef != NULL) {
  83.     free(userdef);
  84.     userdef = NULL;
  85.   }
  86.  
  87.   /* Call parent functions if multiple callbacks are chained. */
  88.   if (CALLBACKARG_PREV_FUN(carg, end) != NULL) {
  89.     return CALLBACKARG_PREV_FUN(carg, end)(CALLBACKARG_PREV_CARG(carg), opt);
  90.   }
  91.  
  92.   return 1;  /* success */
  93. }
  94.  
  95. static int mysavename(t_hts_callbackarg *carg, httrackp *opt, const char* adr_complete, const char* fil_complete, const char* referer_adr, const char* referer_fil, char* save) {
  96.   t_my_userdef *userdef = (t_my_userdef*) CALLBACKARG_USERDEF(carg);
  97.   char * const string1 = userdef->string1;
  98.   char * const string2 = userdef->string2;
  99.   /* */
  100.   char *buff, *a, *b;
  101.  
  102.   /* Call parent functions if multiple callbacks are chained. */
  103.   if (CALLBACKARG_PREV_FUN(carg, savename) != NULL) {
  104.     if (!CALLBACKARG_PREV_FUN(carg, savename)(CALLBACKARG_PREV_CARG(carg), opt, adr_complete, fil_complete, referer_adr, referer_fil, save)) {
  105.       return 0;  /* Abort */
  106.     }
  107.   }
  108.  
  109.   /* Process */
  110.   buff = strdup(save);
  111.   a = buff;
  112.   b = save;
  113.   *b = '\0';          /* the "save" variable points to a buffer with "sufficient" space */
  114.   while(*a) {
  115.     if (strncmp(a, string1, (int)strlen(string1)) == 0) {
  116.       strcat(b, string2);
  117.       b += strlen(b);
  118.       a += strlen(string1);
  119.     } else {
  120.       *b++ = *a++;
  121.       *b = '\0';
  122.     }
  123.   }
  124.   free(buff);
  125.  
  126.   return 1;  /* success */
  127. }
  128.